﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmCelula : Form
    {
        [Dependency]
        public ICelulaBL CelulaBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmCelula()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        public USUARIO currentUsuarioCelula { get; set; }

        private void FrmCelula_Load(object sender, EventArgs e)
        {
            if (currentUsuarioCelula != null)
            {
                menuLlena();
            }
            else
                CargaData();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioCelula.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.BtnEliminar,this.BtnEditar,this.BtnNuevo};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargaData();
        }

        private void CargaData()
        {
            cELULABindingSource.DataSource = CelulaBL.GetAllFromCelulas();
        }

        public CELULA CurrentCelula { get; set; }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            cELULABindingSource.DataSource = CelulaBL.GetFromCelulaByCriterio(txtCriterio.Text);
        }

        private void BtnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoCelula frm = new FrmMttoCelula();
            frm.AddCelula();
            frm.ShowDialog();
            CargaData();
        }

        private void BtnEditar_Click(object sender, EventArgs e)
        {
            CELULA updtCelula = cELULABindingSource.Current as CELULA;
            if (updtCelula != null)
            {
                FrmMttoCelula frmmttoCelula = new FrmMttoCelula();
                frmmttoCelula._celula = updtCelula;
                frmmttoCelula.UpdateCelula();
                frmmttoCelula.ShowDialog();
                this.CargaData();
            }
            else
            {
                MessageBox.Show("Debe Seleccionar una Célula", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnEliminar_Click(object sender, EventArgs e)
        {
            var celula = cELULABindingSource.Current as CELULA;

            if (celula != null)
            {
                var rpt = MessageBox.Show(this,
                   string.Format("¿Desea Eliminar la Célula: {0}?", celula.Descripcion),
                   "Mensaje",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    CelulaBL.RemoveCelula(celula.Id);
                    CargaData();
                }
            }
            else
            {
                MessageBox.Show("Debe Seleccionar una Célula", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cELULABindingSource.DataSource = CelulaBL.GetFromCelulaByCriterio(txtCriterio.Text);
            }
        }

        private void cELULADataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                CurrentCelula = cELULABindingSource.Current as CELULA;
                Close();
            }
        }
    }
}
